package com.ly.lypopupwindowdemo.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.TextView;

import com.ly.lypopupwindowdemo.R;

import java.io.IOException;

public class JumpActivity extends AppCompatActivity {

    private ImageView image;
    TextView jump_over;

    private int TIME = 4;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:

                    if (TIME > 0) {
                        TIME--;
                    }
                    if (TIME <= 0) {
                        TIME = 4;
                        return;
                    }
                    jump_over.setText("跳过" + TIME + "S");
                    handler.sendEmptyMessageDelayed(1, 1000);
                    break;
            }
            super.handleMessage(msg);
        }
    };
    private runble runble;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_jump);

        jump_over = (TextView) findViewById(R.id.id_jump_over);
        image = (ImageView) findViewById(R.id.imageview);

        AlphaAnimation anima = new AlphaAnimation(1.0f, 1.0f);
        anima.setDuration(0);// 设置动画显示时间
        image.startAnimation(anima);
        anima.setAnimationListener(new AnimationImpl());

    }

    private class AnimationImpl implements Animation.AnimationListener {

        @Override
        public void onAnimationStart(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            try {
                startMain(); // 动画结束后跳转到别的页面
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }
    }

    private void startMain() throws IOException {

        handler.sendEmptyMessage(1);
        runble = new runble();
        handler.postDelayed(runble, 2000);

        jump_over.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handler.removeCallbacks(runble);
                finish();
            }
        });

    }

    class runble implements Runnable {

        @Override
        public void run() {
            finish();
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        handler.removeCallbacks(runble);
        finish();
    }

}
